package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.constant.AdvertManage;
import com.qire.common.constant.DeviceConfigInfo;
import com.qire.common.constant.StartupConfigInfo;
import com.qire.common.constant.TeensModeManage;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.model.enumeration.RewardType;
import com.qire.common.model.enumeration.SpecialEventType;
import com.qire.manhua.dal.IAppConfigDAL;
import com.qire.manhua.model.vo.GlobalAdvertConfigVO;
import com.qire.manhua.model.vo.ProtocolVarVO;
import com.qire.manhua.model.vo.StartupConfigVO;
import com.qire.manhua.model.vo.StartupConfigVO.ActivityVO;
import com.qire.manhua.model.vo.StartupConfigVO.WelfareVO;
import com.qire.other.advert.Advertiser;

import java.util.Map;

/**
 * 同步服务器app相关全局配置信息
 */
public class SyncServerConfigService extends DataService {

    private IAppConfigDAL appConfigDAL = WarehouseFactory.create(this, IAppConfigDAL.class);

    public void syncConfig() {
        // 服务器协议配置
        appConfigDAL.fetchProtocolVar();
        // 服务器全局广告配置
        appConfigDAL.fetchGlobalAdvertConfig();
        // 服务器启动配置信息
        appConfigDAL.fetchStartupConfig();
    }
    @StrategyAnnotation(event = Url.ProtocolVar)
    public void fetchProtocolVarApiCallback(ProtocolVarVO dataVO, boolean isCache) {
        if(ObjectUtil.nonNull(dataVO)) {
            DeviceConfigInfo.updateProtocolVer(dataVO.fuwuxieyi, dataVO.yinsixieyi);
        }
    }
    @StrategyAnnotation(event = Url.GlobalAdvertConfig)
    public void fetchProtocolVarApiCallback(Map<String, GlobalAdvertConfigVO> dataVO, boolean isCache) {
        for(GlobalAdvertConfigVO configVO : dataVO.values()) {
            if(StringUtil.notEmpty(configVO.csj_pid)) {
                AdvertManage.findAdvertSpace(configVO.position).putAdvert(Advertiser.PANGOLIN, configVO.csj_pid, 1, 1);
//                AdvertManage.findAdvertSpace(configVO.position).putAdvert(Advertiser.PANGOLIN, "945741121", 1, 1);
            }
        }
    }
    @StrategyAnnotation(event = Url.StartupConfig)
    public void fetchStartupConfigCallback(StartupConfigVO dataVO, boolean isCache) {
        if(ObjectUtil.isNull(dataVO)) {
            return;
        }

        // 启动时的促销活动
        ActivityVO promotionalEvent = dataVO.activity;
        if(ObjectUtil.nonNull(promotionalEvent)) {
            boolean isEnable    = StringUtil.valueToInt(promotionalEvent.is_open, 0) == 1;
            SpecialEventType type = SpecialEventType.build(promotionalEvent.activity_type);
            String  imgUrl      = promotionalEvent.image_url;
            int     imgWidth    = StringUtil.valueToInt(promotionalEvent.image_w, 0);
            int     imgHeight   = StringUtil.valueToInt(promotionalEvent.image_h, 0);
            String  eventUrl    = promotionalEvent.activity_url;
            String  eventTitle  = promotionalEvent.title;
            int     comicId     = promotionalEvent.activity_bid;
            String  startTime   = promotionalEvent.start_time + " 00:00:00";
            String  endTime     = promotionalEvent.end_time   + " 23:59:59";
            StartupConfigInfo.INFO.loadPromotionalEventInfo(isEnable, type, imgUrl, imgWidth, imgHeight, eventUrl, eventTitle, comicId, startTime, endTime);
        }

        // 启动时的首页浮层奖励任务
        WelfareVO welfareVO = dataVO.welfare;
        if(ObjectUtil.nonNull(welfareVO)) {
            RewardType  loginRewardType     = RewardType.build(welfareVO.nua_type);
            int         loginRewardValue    = welfareVO.nua_num;
            RewardType  bindRewardType      = RewardType.build(welfareVO.ba_type);
            int         bindRewardValue     = welfareVO.ba_num;
            StartupConfigInfo.INFO.loadUserWelfareTaskInfo(loginRewardType, loginRewardValue, bindRewardType, bindRewardValue);

            TeensModeManage.updateReadLimitTime(StringUtil.valueToInt(welfareVO.tee_time, 40));
        }

        // 设置客服QQ
        DeviceConfigInfo.qqNumberBySyncServer(dataVO.qq);
    }

}
